<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'
import '@/mork/mockData'

const router = useRouter()

// 定义书法家数据结构
interface Calligrapher {
  id: string
  name: string
  brief_introduction: string
  head_portrait: string
}

// 书法家数据
const calligraphers = ref<Calligrapher[]>([])

// 获取书法家数据
const fetchCalligraphers = async () => {
  try {
    const response = await axios.get('http://localhost:8080/selectAllImages')
    const data = response.data.data

    // 使用 Map 去重,整理书法家数据
    const authorsMap = new Map()

    data.forEach((work: any) => {
      if (!authorsMap.has(work.author)) {
        authorsMap.set(work.author, {
          id: work.id,
          name: work.author,
          brief_introduction: work.brief_introduction,
          head_portrait: work.head_portrait
        })
      }
    })

    calligraphers.value = Array.from(authorsMap.values())
  } catch (error) {
    console.error('获取书法家数据失败:', error)
  }
}

// 页面加载时获取数据
onMounted(() => {
  fetchCalligraphers()
})

// 搜索功能
const searchQuery = ref('')

const filteredCalligraphers = computed(() => {
  if (!searchQuery.value) return calligraphers.value
  return calligraphers.value.filter(calligrapher =>
    calligrapher.name.includes(searchQuery.value) ||
    calligrapher.brief_introduction.includes(searchQuery.value)
  )
})

// 跳转到书法家详情页
const goToDetail = (id: string) => {
  router.push({
    name: 'CalligrapherDetail',
    params: { id: id.toString() } // 确保 id 是字符串
  });
};

</script>

<template>
  <div class="main-body">
    <div class="calligrapherList">
      <!-- 页头部分 -->
      <div class="header">
        <h1>书法家列表</h1>
      </div>

      <!-- 搜索框 -->
      <div class="search-section">
        <el-input v-model="searchQuery" placeholder="搜索..." class="search-input">
          <template #suffix>
            <el-icon>
              <Search />
            </el-icon>
          </template>
        </el-input>
        <el-button class="menu-button">
          <el-icon>
            <Menu />
          </el-icon>
        </el-button>
      </div>

      <!-- 书法家展示区域 -->
      <div class="list">
        <div v-for="calligrapher in filteredCalligraphers" :key="calligrapher.id" class="calligrapher-card"
          @click="goToDetail(calligrapher.id)">
          <img :src="calligrapher.head_portrait" alt="书法家头像" class="calligrapher-image" />
          <div class="card-content">
            <h2>{{ calligrapher.name }}</h2>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.main-body {
  margin-left: 100px;
  padding: 40px;
  height: calc(100vh - 80px);
  box-sizing: border-box;
  display: flex;
  justify-content: center;
  overflow: hidden;
}

.calligrapherList {
  font-family: 'Arial', sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding: 20px;
}

.header {
  text-align: center;
  font-size: 26px;
  font-weight: bold;
  color: #333;
  margin: 0;
}

.search-section {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 500px;
  margin-bottom: 50px;
  background-color: #fff;
  border-radius: 50px;
  padding: 3px 15px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.list {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  /* 默认5个卡片每行 */
  gap: 50px;
  width: 100%;
}

.calligrapher-card {
  background-color: #f9f9f9;
  border: 1px solid #ddd;
  border-radius: 10px;
  cursor: pointer;
  overflow: hidden;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s;
  width: 200px;
  /* 固定卡片宽度 */
  height: 250px;
  /* 固定卡片高度 */
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
}

.calligrapher-card:hover {
  transform: translateY(-10px);
}

.calligrapher-image {
  width: 180px;
  height: 180px;
  object-fit: cover;
  border-radius: 50%;
  margin-top: 10px;
}

.card-content {
  padding: 10px;
  text-align: center;
}

.card-content h2 {
  font-size: 1.2rem;
  font-weight: 600;
  margin-top: 1px;
}

.card-content p {
  font-size: 1rem;
  color: #555;
}

/* 响应式布局调整 */
@media (max-width: 1200px) {
  .list {
    grid-template-columns: repeat(4, 1fr);
    /* 屏幕宽度1200px以下，每行4个 */
  }
}

@media (max-width: 800px) {
  .list {
    grid-template-columns: repeat(3, 1fr);
    /* 屏幕宽度900px以下，每行3个 */
  }
}

@media (max-width: 600px) {
  .list {
    grid-template-columns: repeat(2, 1fr);
    /* 屏幕宽度600px以下，每行2个 */
  }
}
</style>
